<template>
  <div class="home-container">
    <div class="header">
      <h1>欢迎回来！</h1>
      <nut-button type="danger" size="small" @click="handleLogout">退出登录</nut-button>
    </div>
    
    <div class="content">
      <nut-card title="体育应用首页" :border="true">
        <p class="welcome-text">您已成功登录体育应用系统</p>
        <p class="user-info">当前用户：{{ currentUser }}</p>
        <p class="login-time">登录时间：{{ loginTime }}</p>
      </nut-card>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { showToast } from '@nutui/nutui'

const router = useRouter()
const currentUser = ref('')
const loginTime = ref('')

// 处理退出登录
const handleLogout = () => {
  // 清除localStorage中的token
  localStorage.removeItem('token')
  localStorage.removeItem('username')
  
  // 显示退出成功提示
  showToast.success('已退出登录')
  
  // 跳转到登录页
  router.push('/login')
}

// 格式化时间
const formatDateTime = (date: Date): string => {
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')
  const seconds = String(date.getSeconds()).padStart(2, '0')
  
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

// 组件挂载时初始化数据
onMounted(() => {
  // 获取用户名
  currentUser.value = localStorage.getItem('username') || '未知用户'
  
  // 设置登录时间（这里使用当前时间作为示例）
  loginTime.value = formatDateTime(new Date())
})
</script>

<style scoped lang="scss">
.home-container {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  
  h1 {
    font-size: 20px;
    color: #333;
  }
}

.content {
  .nut-card {
    width: 100%;
  }
  
  .welcome-text {
    font-size: 16px;
    color: #1890ff;
    margin-bottom: 10px;
  }
  
  .user-info,
  .login-time {
    font-size: 14px;
    color: #666;
    margin-bottom: 8px;
  }
}
</style>
